<?php $this->load->view("common/page_header");?>
<link href="<?php echo site_url();?>css/uploadify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="<?php echo site_url();?>js/swfobject.js"></script>
<script type="text/javascript" src="<?php echo site_url();?>js/jquery.uploadify.v2.1.4.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('#file_upload').uploadify({
    'uploader'  : '<?php echo site_url();?>img/uploadify.swf',
    'script'    : '<?php echo site_url();?>story/ajax_upload',
    'scriptData': {"SESSION_ID":"<?php echo session_id()?>"},
    'cancelImg' : '<?php echo site_url();?>img/cancel.png',
    'buttonImg' : '<?php echo site_url();?>img/select_file.png',
    'folder'    : '/img/uploads',
    'sizeLimit'   : 4096000,
    'queueSizeLimit': 4,
    'multi':true,
    'auto':false,
    'onComplete'  : function(event, ID, fileObj, response, data) {
	    var ret = eval("(" + response + ")");
	    if(ret.result == 1) {
	    	$('#upload_files').append('<input type="hidden" name="upload_files[]" value="' + ret.name + '`' +ret.rawname + "`" + ret.ext + "`" + ret.type +'"/>');  
	    }
    },
    'onError'     : function (event,ID,fileObj,errorObj) {
        alert(errorObj.type + ' Error: ' + errorObj.info);
      },
    'onAllComplete' : function(event,ID,fileObj,errorObj) {
    	  alert("图片上传完成");
      }  
    
  });
});
</script>
<div id="body_main">	
	<div class="login_header">
		<img class="logo" src="/img/logo2.png" />
		<div class="title">发布事项</div>
		<div class="action"><a href="/user/show">行动中心</a></div>
		<div class="clear"></div>
	</div>
	<div class="login_body" style="background:none">
		<div class="arrow_up">
			<em>◆</em>
			<span>◆</span>
		</div>
		<?php echo validation_errors(); ?>
		<form action="/story/create" id="story-form" class="margin_top15 margin_left15" method="post" enctype="multipart/form-data" onsubmit="return is_form_valid();"> 
			<div class="form_row">
				<label class="input_label"><span class="red">* </span>发布对象：</label>
				<input type="radio" class="input_radio" name="is_public" value="1" <?php echo empty($is_public) ? '' : 'checked';?>/> 公开
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<input type="radio" class="input_radio" name="is_public" value="0" <?php echo empty($is_public) ? 'checked' : '';?>/> 验证会员可见
			</div>
			<div class="form_row">
				<label class="input_label"><span class="red">* </span>事项类别：</label>
				<select class="section_selector">
					<option value=""><span class="red">* </span>行业类别</option>
					<?php foreach($first_level_categories as $category) {?>
					<option value="<?php echo $category['id']?>" <?php if(!empty($first_level_category_id) && $first_level_category_id == $category['id']) echo "selected";?>><?php echo $category['name']?></option>	
					<?php }?>
				</select>
				<select name="category_id" id="category_id">
					<option value=""><span class="red">* </span>事项分类</option>
					
				</select> 
			</div>
			<div class="form_row">
				<label class="input_label"><span class="red">* </span>发布地区：</label>
				<?php $this->load->view("common/province_selector");?>
			</div>
			<div class="form_row">
				<label class="input_label"><span class="red">* </span>酬金设定：</label>
				<input name="bonus" type=text" value="<?php echo empty($bonus) ? '' : $bonus;?>" class="input_text number"/>&nbsp;&nbsp;&nbsp;元
			</div>
			<div class="form_row">
				<label class="input_label"><span class="red">* </span>期限设定：</label>
				<input name="time_cost" type=text" value="<?php echo empty($time_cost) ? '' : $time_cost;?>" class="input_text number"/>&nbsp;&nbsp;&nbsp;天
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="grey">(阿拉伯数字整数，少于一天按一天填写)</span>
			</div>
			<div class="form_row">
				<label class="input_label"><span class="red">* </span>事项标题：</label>
				<input name="title" type=text" id="story_title" class="input_text" value="<?php echo empty($title) ? '' : $title;?>"/>
				<div class="input_hint">请不要超过25字</div>
				<div class="form_error"><div class="error_icon"></div>事项标题请不要超过25个字</div>
			</div>
			<div class="form_row">
				<label class="input_label"><span class="red">* </span>具体要求：</label>
				<textarea name="introduction" id="story_introduction" class="input_textarea"><?php echo empty($introduction) ? '' : $introduction;?></textarea>
				<div class="input_hint">10-2000字</div>
				<div class="form_error"><div class="error_icon"></div>具体要求要在10-2000个字内</div>
			</div>
			<div class="form_row">
				<label class="input_label">联系方式：</label>
				<input name="contact" type=text" class="input_text" value="<?php echo empty($contact) ? '' : $contact;?>"/>
			</div>
			<div class="form_row">
				<label class="input_label"></label>
<!--				<input type="file" name="userfile" size="20" value="上传图片"/>
可上传 4张图片，每张最大1M，支持jpg/gif/bmp/png格式-->
			<input id="file_upload" name="file_upload" type="file" value="选择文件" />
				<span style="padding-left:80px;font-size:14px;font-weight:bold;"><a href="javascript:$('#file_upload').uploadifyUpload();">上传图片</a></span>
			</div>
			<div id="upload_files"></div>
			<div class="form_action">
				<?php if(!empty($id)) {?>
					<input type="hidden" name="id" value="<?php echo $id;?>"></input>
				<?php }?>
				<?php if(!empty($user_id)) {?>
					<input type="hidden" name="user_id" value="<?php echo $user_id;?>"></input>
				<?php }?>
				<input type="button" value="立即发布" class="input_button" id="submit_button"/>
			</div>
			
		</form>
		<div class="clear"></div>
	</div>
</div>
<?php $this->load->view("common/footer");?>		
<?php $this->load->view("common/page_footer");?>
<script type="text/javascript">
function is_form_valid() {
	var has_error = false;

	var story_title = $('#story_title');
	var title = story_title.val();
	if (title == '') {
		has_error = true;
		story_title.parent().find('.form_error').html('<div class="error_icon"></div>事项标题不能为空').show();
	} else if (title.length > 25) {
		has_error = true;
		story_title.parent().find('.form_error').html('<div class="error_icon"></div>事项标题要在25个字内').show();
	} else {
		story_title.parent().find('.form_error').hide();
	}

	var story_introduction = $('#story_introduction');
	var introduction = story_introduction.val();
	if (introduction.length < 10 || introduction.length > 2000) {
		has_error = true;
		story_introduction.parent().find('.form_error').show();
	} else {
		story_introduction.parent().find('.form_error').hide();
	}

	return !has_error;
}
$(document).ready(function(){
	$("#register_button").colorbox({iframe:true, innerWidth:556, innerHeight:320, close:'关闭'});
	$("#login_button").colorbox({iframe:true, innerWidth:390, innerHeight:180, close:'关闭'});
	$("#submit_button").click(function(){
		//$('#file_upload').uploadifyUpload();
		$('#story-form').submit();
	});
	<?php if(!empty($category_id)) {?>
	var first_level_category_id = <?php echo $first_level_category_id;?>;
	$.get( '/front/find_sub_categories_ajax', {section_id:<?php echo $first_level_category_id?>}, function(data) {
		var ret = eval('(' + data + ')'); 
		var html = '';
		for (var x in ret) {
			html += '<option value="' + ret[x].id + '"' + (ret[x].id == <?php echo $category_id;?> ? 'selected' : '')+ '>' + ret[x].name + '</option>';
		}
		$('#category_id').html(html);
		
	});
	<?php }?>
});	
</script>